<?php
/**
 * User: CQH
 * Date: 2017/8/28
 * Time: 09:48
 */
namespace app\common\model;

use think\Model;
use think\db;
/*
 * 客户与行政中心关联表信息model
 */
class CustomerAdministrativeCenter extends Model
{
	protected $db;

	public function __construct()
	{
		parent::__construct();
		$this->db = Db::name("CustomerAdministrativeCenter");
	}

	/**
	 * 客户与行政中心关联表记录增加一条数据
	 * @param $data  ->  数组   ['key' => 'value']
	 * @return bool
	 */
	public function insertOne($data)
	{
        //$this->data($data);
        $result = $this->db->insert($data);
        // 判断结果 并返回
        if ($result !== 1) {
            return false;
        }
        return $result;
	}

	/**
	 * 按id主键删除一条数据
	 * @param $id
	 * @return $this|bool
	 */
	public function deleteOne($id)
	{
		$result = $this->db->where(array('id'=>$id))->delete();
		if ($result !== 1) {
			return false;
		}
		return $result;
	}

	/**
	 * 根据条件 $where修改一条数据
	 * @param $where ->  条件  ['key' => 'value']
	 * @param $data  ->  数据  ['key' => 'value', 'key' => 'value']
	 * @return $this|bool
	 */
    public function updateOne($where,$data)
    {
    	// 根据条件修改一条数据
    	$result = $this->db->where($where)->update($data);
    	// 判断结果 并返回数据
    	if ($result == false) {
    		return false;
    	}
    	return $result;
    }

    /**
     * 根据条件 $where获取单条数据
     * @param $where  ->  数组  ['key' => 'value']
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function selectOne($where)
    {
    	$userInfo = $this->db->where($where)->cache(true)->select();
    	// 判断结果并返回
    	if ($userInfo == false) {
    		return false;
    	}
    	return $userInfo;
    }

    /**
     * 获取所有数据
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getList()
    {
    	$userInfo = $this->db->cache(true)->select();
    	// 判断结果 并返回
    	if ($userInfo == false) {
    		return false;
    	}
    	return $userInfo;
    }

    /**
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     *     根据条件，获取所有数据
     */
    public function getAll($where = [], $nowPage = '', $limits = '', $field = '*',$join=array(),$group='')
    {
        if(empty($group)){
             $data = $this->db
                ->field($field)
                ->where($where)
                ->join($join)
                ->page($nowPage,$limits)
                ->select();
            }else{
            $data = $this->db
                ->field($field)
                ->where($where)
                ->join($join)->group($group)
                ->page($nowPage,$limits)
                ->select();
            }

        return $data;

    }
    public function count($where,$join,$group=''){
        if(empty($group)){
            return $this->db->where($where)->join($join)->count();
        }else{
            return $this->db->where($where)->join($join)->group($group)->count();
        }

    }

}
